package cn.sxt.userscenter.users.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.userscenter.common.util.ConstatFinaUtil;
import cn.sxt.userscenter.common.util.PageInfoUtil;
import cn.sxt.userscenter.users.dao.IAAdminsDao;
import cn.sxt.userscenter.users.dao.IARoleDao;
import cn.sxt.userscenter.users.dao.IAUsersDao;
import cn.sxt.userscenter.users.dao.IAUsersExtendDao;
import cn.sxt.userscenter.users.pojo.AAdmins;
import cn.sxt.userscenter.users.pojo.ARole;
import cn.sxt.userscenter.users.pojo.AUsers;
import cn.sxt.userscenter.users.pojo.AUsersExtend;
import cn.sxt.userscenter.users.service.IUsersService;
@Service("usersService")
public class UsersServiceImpl implements IUsersService{

	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao ;
	@Resource
	private IAUsersDao usersDao ;
	@Resource
	private IAUsersExtendDao usersExtendDao ;
	
	/* 管理员模块开始 */
	/**
	 * 查询一条管理员
	 */
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {
		return this.adminsDao.findOne(condMap);
	}

	/**
	 * 查询多条管理员
	 */
	@Override
	public List<AAdmins> findListAdminsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil != null)
		{
			/* 查询分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAdmins> adminsList = this.adminsDao.findList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return adminsList ; 
		}
		/* 查询不分页 */
		return this.adminsDao.findList(condMap);
	}

	/**
	 * 保存一条管理员
	 */
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.adminsDao.saveOne(admins);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "1");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 更新一条管理员
	 */
	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.adminsDao.updateOne(admins);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 删除一条管理员
	 */
	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.adminsDao.deleteOne(admins);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	/* 管理员模块结束 */
	
	/* 角色模块开始 */
	/**
	 * 查询一条角色
	 */
	@Override
	public ARole findOneRoleService(Map<String, Object> condMap) {
		return this.roleDao.findOne(condMap);
	}

	/**
	 * 查询多条角色
	 */
	@Override
	public List<ARole> findListRoleService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil != null)
		{
			/* 查询分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<ARole> roleList = this.roleDao.findList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return roleList ; 
		}
		/* 查询不分页 */
		return this.roleDao.findList(condMap);
	}

	/**
	 * 保存一条角色
	 */
	@Override
	public JSONObject saveOneRoleService(ARole role) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.roleDao.saveOne(role);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", role.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "1");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 更新一条角色
	 */
	@Override
	public JSONObject updateOneRoleService(ARole role) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.roleDao.updateOne(role);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", role.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 删除一条角色
	 */
	@Override
	public JSONObject deleteOneRoleService(ARole role) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.roleDao.deleteOne(role);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", role.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	/* 角色模块结束 */
	
	/* 用户模块开始 */
	/**
	 * 查询一条用户
	 */
	@Override
	public AUsers findOneUsersService(Map<String, Object> condMap) {
		return this.usersDao.findOne(condMap);
	}

	/**
	 * 查询多条用户
	 */
	@Override
	public List<AUsers> findListUsersService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil != null)
		{
			/* 查询分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AUsers> usersList = this.usersDao.findList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return usersList ; 
		}
		/* 查询不分页 */
		return this.usersDao.findList(condMap);
	}

	/**
	 * 保存一条用户
	 */
	@Override
	public JSONObject saveOneUsersService(AUsers users) {
		JSONObject resultJSON = new JSONObject(); 
		/* 检验邮箱是否重复 */
		Map<String,Object> condMap = new HashMap();
		condMap.put("email", users.getEmail());
		AUsers usersRes = this.findOneUsersService(condMap);
		if(usersRes != null)
		{
			/* 邮箱已经存在了 */
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinaUtil.INFO_JSON.get("2"));
			return resultJSON ; 
		}
		int res = this.usersDao.saveOne(users);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "1");
			resultJSON.put("info", "成功");
			
			/* 还要保存用户的扩展信息 */
			AUsersExtend usersExtend = new AUsersExtend() ; 
			usersExtend.setUsersId(users.getId());
			usersExtend.setCreateTime(new Date());
			usersExtend.setUpdateTime(new Date());
			users.setUsersExtend(usersExtend);
			
			this.saveOneUsersExtendService(usersExtend);
		}else
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 更新一条用户
	 */
	@Override
	public JSONObject updateOneUsersService(AUsers users) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.usersDao.updateOne(users);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			AUsersExtend usersExtend = users.getUsersExtend() ; 
			if(usersExtend != null)
			{
				this.updateOneUsersExtendService(usersExtend);
			}
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 删除一条用户
	 */
	@Override
	public JSONObject deleteOneUsersService(AUsers users) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.usersDao.deleteOne(users);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	/* 用户模块结束 */
	
	/* 用户扩展模块开始 */
	/**
	 * 查询一条用户扩展
	 */
	@Override
	public AUsersExtend findOneUsersExtendService(Map<String, Object> condMap) {
		return this.usersExtendDao.findOne(condMap);
	}

	/**
	 * 查询多条用户扩展
	 */
	@Override
	public List<AUsersExtend> findListUsersExtendService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil != null)
		{
			/* 查询分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AUsersExtend> usersExtendList = this.usersExtendDao.findList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return usersExtendList ; 
		}
		/* 查询不分页 */
		return this.usersExtendDao.findList(condMap);
	}

	/**
	 * 保存一条用户扩展
	 */
	@Override
	public JSONObject saveOneUsersExtendService(AUsersExtend usersExtend) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.usersExtendDao.saveOne(usersExtend);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", usersExtend.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "1");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 更新一条用户扩展
	 */
	@Override
	public JSONObject updateOneUsersExtendService(AUsersExtend usersExtend) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.usersExtendDao.updateOne(usersExtend);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", usersExtend.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	/**
	 * 删除一条用户扩展
	 */
	@Override
	public JSONObject deleteOneUsersExtendService(AUsersExtend usersExtend) {
		JSONObject resultJSON = new JSONObject(); 
		int res = this.usersExtendDao.deleteOne(usersExtend);
		if(res > 0 )
		{
			/* 存储数据 */
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("id", usersExtend.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	/* 用户扩展模块结束 */
}
